package umd.mhealth.lab4;



import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MyFragment extends Fragment {
	private ViewGroup rootView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		rootView = (ViewGroup) inflater.inflate(R.layout.my_fragment, container, false);

		final Button buttonOne = (Button) rootView.findViewById(R.id.button_one);
		buttonOne.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				TextView tv = (TextView) rootView.findViewById(R.id.tv);	//accessing text in the textview
				CharSequence value = tv.getText();
				new CountTask().execute(value);		//sending info to a task onClick()
			}
		});

		final Button buttonTwo = (Button) rootView.findViewById(R.id.button_two);
		buttonTwo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				TextView tv = (TextView) rootView.findViewById(R.id.tv);	//accessing text in the textview
				CharSequence value = tv.getText();
				new CountTask().execute(value);			//sending info to a task onClick()
			}
		});

		if (savedInstanceState != null){
			int bgColor = savedInstanceState.getInt("bgColor");
			TextView view = (TextView) rootView.findViewById(R.id.tv);	
			String text = savedInstanceState.getString("text");		// retrieving text as well now
			view.setBackgroundColor(bgColor);
			view.setText(text);
		}

		return rootView;
	}

	@Override
	public void onSaveInstanceState(Bundle savedInstanceState){
		super.onSaveInstanceState(savedInstanceState);
		TextView tv = (TextView) rootView.findViewById(R.id.tv); // targeting a new View to save info from
		int bgColor = Color.TRANSPARENT;
		Drawable bg = (Drawable) tv.getBackground();
		if (bg instanceof ColorDrawable)
			bgColor = ((ColorDrawable) bg).getColor();
		CharSequence text =  tv.getText();	// saving text as well now
		savedInstanceState.putInt("bgColor", bgColor);
		savedInstanceState.putCharSequence("text", text);
	}
	
	/* Writing the AsyncTask CountTask */
	private class CountTask extends AsyncTask<CharSequence, Void, Integer> {
		@Override
		protected Integer doInBackground(CharSequence...params) {
			Integer result = Integer.valueOf(params[0].toString());
			
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			return result + 1;
		}
		
		public void onPostExecute(Integer result){
			int color = Color.WHITE;
			
			if ((result % 2) == 0) {
				color = Color.BLUE;
			} else {
				color = Color.RED;
			}
			
			TextView tv = (TextView) rootView.findViewById(R.id.tv);	//setting the display
			tv.setText(result.toString());
			tv.setBackgroundColor(color);
		}
	}	
}



